import 'package:casdon/modules/home/data/model/home_banner_module.dart';
import 'package:casdon/modules/home/domain/states/home_banner_list_state.dart';
import 'package:casdon/modules/home/providers/home_provider.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class HomeBannerListNotifier extends AsyncNotifier<HomeBannerListState> {
  @override
  Future<HomeBannerListState> build() async {
    return _fetchData();
  }

  Future<void> refresh() async {
    try {
      final newState = await _fetchData();
      state = AsyncValue.data(newState);
    } catch (e, st) {
      state = AsyncValue.error(e, st);
    }
  }

  Future<HomeBannerListState> _fetchData() async {
    final ret = await ref.read(homeBannerListUsercaseProvider).execute(); 
    final data = HomeBannerModule.listFromApi(ret);
    data.sort((a, b) => a.sort.compareTo(b.sort));
    return HomeBannerListState(data: data);
  }
}